package dtaskboard.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import dtaskboard.client.dto.ProjectDTO;
import dtaskboard.client.dto.SprintDTO;

@Entity
public class Project {

  private Long id;
  private String name;
  private Long hours;
  private Long estimatedSprints;
  private List<Sprint> sprints;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Column(unique = true)
  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Long getHours() {
    return this.hours;
  }

  public void setHours(Long hours) {
    this.hours = hours;
  }

  public Long getEstimatedSprints() {
    return this.estimatedSprints;
  }

  public void setEstimatedSprints(Long estimatedSprints) {
    this.estimatedSprints = estimatedSprints;
  }

  @OneToMany
  public List<Sprint> getSprints() {
    if (this.sprints == null) {
      this.sprints = new ArrayList<Sprint>();
    }
    return this.sprints;
  }

  public void setSprints(List<Sprint> sprints) {
    this.sprints = sprints;
  }

  public ProjectDTO toDTO() {
    ProjectDTO dto = new ProjectDTO();
    dto.setEstimatedSprints(this.estimatedSprints);
    dto.setHours(this.hours);
    dto.setName(this.name);

    List<SprintDTO> sprints = new ArrayList<SprintDTO>();
    for (Sprint sprint : this.getSprints()) {
      sprints.add(sprint.toDTO());
    }

    dto.setSprints(sprints);
    return dto;
  }

}
